"""
Case Type   : 导入服务器时修改服务器名称_名称改变
Case Name   : 导入服务器时单个标签纯数字导入_名称改变
Create At   : 2025/05/10
Owner       : @l1azzzy
Description :
    1.传入数据
    2.调用接口
    3.获取返回值
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""

import os
import unittest
import logging
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.datakit_api.BaseOpsApi import BaseOpsApi
from openpyxl import Workbook, load_workbook
import pandas as pd
import requests

class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.base_ops_api = BaseOpsApi(self.token)
        self.pri_node = Node('PrimaryDbUser')
        self.pri_node_root = Node('PrimaryRoot')
        self.server_name = 'test_001'

def test_upload_server():
    server_data = [
        None,  
        "test_host002",        
        {self.pri_node_root.ssh_host},               
        {self.pri_node_root.ssh_host},
        {self.pri_node_root.ssh_port},
        "root",         
        {self.pri_node_root.ssh_password},
        "否",
        "性能测试环境", 
        "自动化测试生成的数据" 
    ]
    
    excel_path = ExcelHandler.write_server_data(data=server_data)

    print(f"[INFO] 测试数据已写入文件: {excel_path}")

    files = {
        'file': ('servers.xlsx', open('{excel_path}', 'rb'), 'text/plain') 
    }

    response = requests.post(url, headers=headers, files=files, verify=False)
    
    if response.status_code == 200:
        print("[SUCCESS] 上传成功！响应结果:")
        print(response.json())
    else:
        print(f"[ERROR] 上传失败！状态码: {response.status_code}")
        print("错误详情:", response.text)

    assert response.status_code == 200, f"上传失败！状态码: {response.status_code}"